package com.behome.netty.netty_B.bean;


import cn.hutool.json.JSONObject;
import com.behome.netty.common.ByteConventerKit;
import com.behome.netty.netty_B.enums.ReBackAnnotation;
import com.behome.netty.netty_B.enums.ReBackCmdType;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;


@ReBackAnnotation(
        type = ReBackCmdType.BROADCAST_RESULT
)
public class TerminalBroadcastResult extends BusinessReBackBase {
    private String ebmId;
    private byte resultCode;
    private String desc;
    private Date beginTime;
    private Date endTime;
    private byte broadcastCount;
    private Date time;

    public TerminalBroadcastResult() {
    }

    @Override
    public boolean validateBeforeSerialize() {
        return false;
    }

    public String getEbmId() {
        return this.ebmId;
    }

    public void setEbmId(String ebmId) {
        this.ebmId = ebmId;
    }

    public Date getTime() {
        return this.time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public byte getResultCode() {
        return this.resultCode;
    }

    public void setResultCode(byte resultCode) {
        this.resultCode = resultCode;
    }

    public String getDesc() {
        return this.desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Date getBeginTime() {
        return this.beginTime;
    }

    public void setBeginTime(Date beginTime) {
        this.beginTime = beginTime;
    }

    public Date getEndTime() {
        return this.endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public byte getBroadcastCount() {
        return this.broadcastCount;
    }

    public void setBroadcastCount(byte broadcastCount) {
        this.broadcastCount = broadcastCount;
    }

    public byte[] serialize() {
        return null;
    }

    public BusinessReBackBase deserialize(byte[] businessData) {
        if (businessData != null && businessData.length >= 34) {
            ByteBuffer byteBuffer = ByteBuffer.wrap(businessData);
            byte[] ebmIdData = new byte[18];
            byteBuffer.get(ebmIdData);
            String ebmId = ByteConventerKit.deserializeEbmId(ebmIdData);
            this.setEbmId(ebmId);
            this.setResultCode(byteBuffer.get());
            short descLength = byteBuffer.getShort();
            byte[] beginTimeData;
            if (descLength > 0) {
                beginTimeData = new byte[descLength];
                byteBuffer.get(beginTimeData);
                this.setDesc(new String(beginTimeData, StandardCharsets.US_ASCII));
            }
            beginTimeData = new byte[4];
            byteBuffer.get(beginTimeData);
            this.setBeginTime(ByteConventerKit.bytesToDatetime(beginTimeData));
            byte[] endTimeData = new byte[4];
            byteBuffer.get(endTimeData);
            this.setEndTime(ByteConventerKit.bytesToDatetime(endTimeData));
            this.setBroadcastCount(byteBuffer.get());
            byte[] timeData = new byte[4];
            byteBuffer.get(timeData);
            this.setTime(ByteConventerKit.bytesToDatetime(timeData));
        }

        return this;
    }


    public String toCHJsonString() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOnce("广播编号", this.ebmId);
        jsonObject.putOnce("播发结果", this.resultCode == 0 ? "失败" : "成功");
        jsonObject.putOnce("结果描述", this.desc);
        jsonObject.putOnce("开始时间", simpleDateFormat.format(this.beginTime));
        jsonObject.putOnce("结束时间", simpleDateFormat.format(this.endTime));
        jsonObject.putOnce("播发次数", this.broadcastCount);
        jsonObject.putOnce("上报时间", simpleDateFormat.format(this.time));
        return jsonObject.toString();
    }
}
